#include "PaletteScene.h"
#include "PaletteShower.h"
#include <QGraphicsSceneMouseEvent>

PaletteScene::PaletteScene(QObject *parent) : QGraphicsScene(parent) {}

PaletteScene::~PaletteScene() = default;

void PaletteScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    int pressed_x = (int) (event->scenePos().x());
    int pressed_y = (int) (event->scenePos().y());
    uint8_t j = pressed_x / 20;
    uint8_t i = pressed_y / 7;
    pickedColorIndex = i + (j * 32);

    if (palette->pickedColorIndex != pickedColorIndex) {
        palette->pickedColorIndex = pickedColorIndex;
        palette->UpdateImage();
    }
}

void PaletteScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    int move_x = (int) event->scenePos().x();
    int move_y = (int) event->scenePos().y();
    uint8_t j = move_x / 20;
    uint8_t i = move_y / 7;
    passedColorIndex = i + (j * 32);

    if (palette->passedColorIndex != passedColorIndex)
    {
        palette->passedColorIndex = passedColorIndex;
        palette->UpdateImage();
    }
}

void PaletteScene::SetPalette(PaletteShower *palette_)
{
    this->palette = palette_;
}
